﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Windows.Forms.Design;
using System.Windows.Forms.ComponentModel;
using System.Windows.Forms.Layout;
using System.Windows.Forms.VisualStyles;
using Microsoft.VisualBasic.PowerPacks;
using Microsoft.VisualBasic.PowerPacks.Printing;

namespace DialogsEditor
{
    public class Line : Control
    {

        public DialogEntity Source { get; set; }
        public DialogEntity Destination { get; set; }
        public bool IsSelected { get; set; }
        private LineEquation equation = new LineEquation();
        public LineShape shape;

        private float a, b;

        public Line(DialogEntity s, DialogEntity d)
        {
            CreateControlsInstance();
            Source = s;
            Destination = d;
            //this.BorderWidth = 5;
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            BackColor = Color.Transparent;
            shape = new LineShape();
            shape.BorderWidth = 5;
            shape.SelectionColor = Color.Transparent;
            shape.MouseDown += shape_MouseDown;
        }

        void shape_MouseDown(object sender, MouseEventArgs e)
        {
            /*foreach (DialogEntity d in Form1.CurrentDialogs)
            {
                d.IsSelected = false;
                d.Refresh();
            }
            foreach (Line d in Form1.CurrentLinks)
            {
                d.IsSelected = false;
                d.Refresh();
                d.RemoveSelection();
            }*/
            Utils.UnselectingAllDialogs();
            Utils.UnselectingAllLinks();
            //if (Form1.IsLinking)
            //{
            //    Form1.ClearCurrentLink();
            //    Form1.Selected = null;
            //}
            //else
            //{
                IsSelected = true;
                shape.BorderColor = Color.Yellow;
                Refresh();
                shape.ResumePaint(true);
            //}
        }

        public void RemoveSelection()
        {
            shape.BorderColor = Color.Black;
            shape.ResumePaint(true);
        }

        /*protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            foreach (Line line in Form1.CurrentLinks)
            {
                line.IsSelected = false;
                line.Refresh();
            }
            if (equation.Contains(PointToClient(Cursor.Position)))
                IsSelected = true;
            //this.BorderColor = Color.Yellow;
            Refresh();
        }*/

        /*public void CheckSelection()
        {
            if (shape.Focused)
                IsSelected = true;
            
            shape.BorderColor = Color.Black;
            if (IsSelected)
                shape.BorderColor = Color.Yellow;

            shape.Invalidate();
            Invalidate();
        }*/

        private bool Contains(Point p)
        {
            if (p.IsEmpty || (a == 0 && b == 0)) return false;
            if (p.Y == a * p.X + b) return true;
            return false;
        }

        public void PaintLine()
        {
            shape.X1 = Source.Position.X;
            shape.Y1 = Source.Position.Y;
            shape.X2 = Destination.Position.X;
            shape.Y2 = Destination.Position.Y;

            //SetPosition();
            //X1 = Source.Position.X;
            //Y1 = Source.Position.Y;
            //X2 = Destination.Position.X;
            //Y2 = Destination.Position.Y;
            //Refresh();
            //e.Graphics.DrawLine(IsSelected ? Form1.PenSelect : Form1.PenUnselect, Source.Position, Destination.Position);

            //a = Math.Abs(Destination.Location.Y - Source.Location.Y) / Math.Abs(Destination.Location.X - Source.Location.X);
            //b = Source.Location.Y - (Source.Location.X * equation.a);
        }


        private class LineEquation
        {
            //equation: y = ax + b

            public float a;
            public float b;

            /*public bool Contains(Point p)
            {
                if (p.Y == a * p.X + b) return true;
                return false;
            }*/

            public bool Contains(Point p)
            {
                if (p.IsEmpty || (a == 0 && b == 0)) return false;
                /*for (int i = -2; i < 2; i++)
                {
                    if (p.Y == a * p.X + (b+i)) return true;
                }*/
                if (p.Y == a * p.X + b) return true;
                return false;
            }
        }

    }
}
